package com.mgq.juc.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * interrupt()和Thread的interrupted()区别
 * <p>
 * 注意:interrupt()不会清除打断标记
 * interrupted() 会清除打断标记
 * 当打断标记为真时,park()方法会失效
 * LockSupport.park()
 */
@Slf4j
public class TestInterruptPark {
    public static void main(String[] args) throws InterruptedException {
        test();
    }

    private static void test() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park.........");
            LockSupport.park();
            log.debug("unPark..........");
            //log.debug("打断状态{}", Thread.currentThread().isInterrupted());
            log.debug("打断状态{}", Thread.interrupted());//interrupted()方法去清除打断标记.也就是把打断标记置为false
            //这个时候 park方法又会重新生效
            LockSupport.park();
            log.debug("unPark..........");
        }, "t1");
        t1.start();
        Thread.sleep(1000);
        t1.interrupt();
    }
}
